/**
 * Alipay.com Inc.
 * Copyright (c) 2004-2022 All Rights Reserved.
 */
package com.example.spider.proxy.provider.pubproxy;

import com.example.spider.proxy.core.ProxyInfo;
import com.example.spider.proxy.core.tester.ProxyTypeEnum;
import com.example.spider.proxy.provider.AbstractProxyResultHandler;
import com.example.spider.proxy.provider.IpProxySpiderTask;
import com.example.spider.starter.SpiderStarter;
import com.example.spider.task.SpiderTaskQueue;
import lombok.Data;

import java.net.Proxy;
import java.time.Duration;
import java.util.Map;

/**
 * http://pubproxy.com/api/proxy JSON 【固定返回随机1条，响应10s+】
 * 主页：http://pubproxy.com/
 *
 * @author lym
 */
//@Component
public class PubProxyResultHandler extends AbstractProxyResultHandler implements SpiderStarter {

    public PubProxyResultHandler() {
        super("pubProxy");
    }

    protected Duration defaultLimitRateDuration() {
        return Duration.ofSeconds(2);
    }

    @Override
    public void startSpiders() {
        // 不限地区
        IpProxySpiderTask task = createTask("http://pubproxy.com/api/proxy");
        SpiderTaskQueue.putTask(task);
    }

    /**
     * 每 5 分钟触发一次
     */
    @Override
    public String executeCron() {
        return "0 */5 * * * ?";
    }

    @Override
    public void doHandle(IpProxySpiderTask task, String json) {

//        List<CoolProxyDTO> list = JsonUtils.parseObject(json, List.class, CoolProxyDTO.class);
//        list.stream().map(CoolProxyDTO::toStandardModel).forEach(proxyInfo -> {
//            // 保存到文件
//            addProxyToSourceRepository(proxyInfo);
//        });
    }

    /**
     * {
     * "data": [
     * {
     * "ipPort": "1.179.144.41:8080",
     * "ip": "1.179.144.41",
     * "port": "8080",
     * "country": "TH",
     * "last_checked": "2022-04-07 19:10:01",
     * "proxy_level": "anonymous",
     * "type": "http",
     * "speed": "14",
     * "support": {
     * "https": 0,
     * "get": 1,
     * "post": 1,
     * "cookies": 1,
     * "referer": 1,
     * "user_agent": 1,
     * "google": 0
     * }
     * }
     * ],
     * "count": 1
     * }
     */
    @Data
    public static class CoolProxyDTO {

        private String ip;

        private Integer port;

        /**
         * 匿名类型 0 透明  1 匿名
         */
        private Integer anonymous;

        /**
         * China
         */
        private String country_name;

        /**
         * CN
         */
        private String country_code;

        /**
         * 时间戳
         * 1649355949.0
         */
        private double update_time;

        private String type;

        /**
         * 成功率
         */
        private double working_average;

        /**
         * 1分
         * 170-180 2分
         * 180-190 3分
         * 190-150 4分
         * 195+ 5分
         */
        private double score;
        /**
         * 响应时间
         */
        private double response_time_average;
        /**
         * 下载速度 kb/s
         */
        private double download_speed_average;


        public ProxyInfo toStandardModel() {
            return ProxyInfo.builder()
                    .source("coolProxy")
                    .ip(ip)
                    .port(port)
                    .anonymity(anonymous == 1 ? "匿名" : "透明")
                    .proxyTypeEnum(ProxyTypeEnum.of(type))
                    .type(ProxyTypeEnum.of(type).getType())
                    .type(Proxy.Type.HTTP)
                    .location(country_code)
                    .responseTime((long) response_time_average)
                    .successPercentage((int) working_average)
                    //.lastVerificationTime(Instant.ofEpochMilli((long) update_time))
                    .extendInfo(Map.of(
                            "download_speed_average", String.valueOf(download_speed_average),
                            "cool_proxy_score", String.valueOf(score)
                    ))
                    .build();
        }

    }
}